﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DNA.Mvc.jQuery;

namespace DNA.Mvc.UI
{
    public class WidgetZone:ContainerViewComponent<Widget>
    {
        public string Title { get; set; }

        public IList<Widget> Widgets
        {
            get { return InnerItems; }
            set { InnerItems = value; }
        }
        public Func<object,object> InlineContent { get; set; }

        public Action Content { get; set; }

        public override void RenderBeginTag(System.Web.UI.HtmlTextWriter writer)
        {
            MergeAttribute("class", "d-widget-zone");
            if (!string.IsNullOrEmpty(Title))
                MergeAttribute("title", Title);
            base.RenderBeginTag(writer);
        }

        public override void RenderContent(System.Web.UI.HtmlTextWriter writer)
        {
            if (Content != null)
                Content.Invoke();
            else
            {
                if (InlineContent != null)
                    writer.Write(InlineContent(null).ToString());
            }

            foreach (var widget in Widgets)
            {
                try
                {
                    if (widget.Model.IsExpanded)
                    {
                        widget.Render(writer);
                    }
                }
                catch(Exception e)
                {
                    continue;
                }
            }

            //base.RenderContent(writer);
        }
    }
}